package com.dbq.share;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.share.WbShareCallback;
import com.sina.weibo.sdk.share.WbShareHandler;
import com.sina.weibo.sdk.utils.Utility;

/**
 * Created by sunkai on 2017/12/26.
 */

public class SinaAssistActivity extends BaseAssistActivity implements WbShareCallback {

    @Override
    public void onWbShareSuccess() {
        shareSuccess();
        finish();
    }

    @Override
    public void onWbShareCancel() {
        shareCancel();
        finish();
    }

    @Override
    public void onWbShareFail() {
        shareError("weibo share error");
        finish();
    }

    private WbShareHandler shareHandler;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WbSdk.install(getApplicationContext(), new AuthInfo(getApplicationContext(), ShareUtil.SINA_APPKEY, ShareUtil.SIAN_REDIRECT_URL, ShareUtil.SINA_SCOPE));
        shareHandler = new WbShareHandler(this);
        shareHandler.registerApp();
    }

    @Override
    protected void appNotInstallTips() {
        Toast.makeText(getApplicationContext(), R.string.dbq_socializ_wb_not_install, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected boolean appInstall() {
        return shareHandler.isWbAppInstalled();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        shareHandler.doResultIntent(intent, this);
    }

    @Override
    protected void sendMessage() {
        if (!isResume) {
            shareCancel();
            return;
        }
        startShare();
        WbShareHandler shareHandler = new WbShareHandler(this);
        shareHandler.registerApp();
        WeiboMultiMessage multiMessage = new WeiboMultiMessage();
        multiMessage.textObject = getTextObj();
        multiMessage.mediaObject = getWebpageObj();
        shareHandler.shareMessage(multiMessage, false);
    }

    /**
     * 创建多媒体（网页）消息对象。
     *
     * @return 多媒体（网页）消息对象。
     */
    private WebpageObject getWebpageObj() {
        WebpageObject mediaObject = new WebpageObject();
        mediaObject.identify = Utility.generateGUID();
        mediaObject.description = shareWebPage.title;
        // 设置 Bitmap 类型的图片到视频对象里         设置缩略图。 注意：最终压缩过的缩略图大小不得超过 32kb。
        mediaObject.setThumbImage(shareWebPage.thumb);
        mediaObject.actionUrl = shareWebPage.url;
        return mediaObject;
    }

    /**
     * 创建文本消息对象。
     *
     * @return 文本消息对象。
     */
    private TextObject getTextObj() {
        TextObject textObject = new TextObject();
        textObject.text = shareWebPage.description;
        return textObject;
    }
}
